package com.gensdai.leliang.common.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.gensdai.leliang.R;
import com.gensdai.leliang.entity.HomeBean;
import com.gensdai.leliang.utils.BaseUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 品牌热榜
 * Created by LT on 2017/6/15.
 */

public class HotBrandAdapterRec extends DelegateAdapter.Adapter<HotBrandAdapterRec.MainViewHolder> {


    private Context mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;
    List<HomeBean.BrandBean> list;


    OnHomeBrandItemClick click;

    public HotBrandAdapterRec(Context context, LayoutHelper layoutHelper, List<HomeBean.BrandBean> list) {
        this(context, layoutHelper, list, new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public HotBrandAdapterRec(Context context, LayoutHelper layoutHelper, List<HomeBean.BrandBean> list, @NonNull VirtualLayoutManager.LayoutParams layoutParams) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.list = list;
        this.mLayoutParams = layoutParams;
    }
    public void setOnItemClick(OnHomeBrandItemClick click){
        this.click = click;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_home_hot_brand, parent, false));
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        holder.itemView.setLayoutParams(
                new VirtualLayoutManager.LayoutParams(mLayoutParams));


        final HomeBean.BrandBean info = list.get(position);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(click != null){
                    click.OnItemClick(info);
                }
            }
        });




        if(position % 2 == 0){
            holder.icon.setBackgroundResource(R.color.image_placebg_color3);
        }else{
            holder.icon.setBackgroundResource(R.color.image_placebg_color4);
        }

        if (TextUtils.isEmpty(info.brandLogo)) {
            Uri uri = Uri.parse("");
            holder.icon.setImageURI(uri);
        } else {
            Uri uri = Uri.parse(info.brandLogo);
            int width = 200, height = 200;
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                    .setResizeOptions(new ResizeOptions(width, height))
                    .build();
            PipelineDraweeController controller = (PipelineDraweeController)Fresco.newDraweeControllerBuilder()
                    .setOldController(holder.icon.getController())
                    .setImageRequest(request)
                    .build();


            holder.icon.setController(BaseUtils.resetPic(uri,holder.icon.getController()));
//            holder.icon.setImageURI(uri);
        }



    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    static class MainViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.icon)
        SimpleDraweeView icon;

        public MainViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
        }
    }

    public interface OnHomeBrandItemClick{
        void OnItemClick(HomeBean.BrandBean bean);
    }

}
